<template>
  <a-modal
    :title="modalTitle"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="菜单ID" :labelCol="labelCol" :wrapperCol="wrapperCol" v-show="false">
          <a-input v-if="modalOpera==0" v-decorator="['id']" />
          <a-input v-if="modalOpera==1" v-decorator="['id', {rules: [{required: true}]}]" />
        </a-form-item>

        <a-form-item label="作者" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['author',{rules: [{required: true, message: '模块名称！'}]}]" />
        </a-form-item>

        <a-form-item label="模块名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['modelName',{rules: [{required: true, message: '模块名称！'}]}]" />
        </a-form-item>

        <a-form-item label="输出地址" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['location',{rules: [{required: true, message: '模块名称！'}]}]" />
        </a-form-item>

        <a-form-item label="是否覆盖" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-radio-group v-decorator="['fileOverride', {rules: [{required: true, message: '请选择是否覆盖'}]}]">
            <a-radio :value="1">是</a-radio>
            <a-radio :value="2">否</a-radio>
          </a-radio-group>
        </a-form-item>

        <a-form-item label="打开生成位置" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-radio-group v-decorator="['open', {rules: [{required: true, message: '请选择是否覆盖'}]}]">
            <a-radio :value="1">是</a-radio>
            <a-radio :value="2">否</a-radio>
          </a-radio-group>
        </a-form-item>

        <a-form-item label="Controller父类路径" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['superControllerClass', {rules: [{required: true, min: 3, message: '请输入路径！'}]}]" />
        </a-form-item>

        <a-form-item label="Service父类路径" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['superServiceClass', {rules: [{required: true, min: 3, message: '请输入路径！'}]}]" />
        </a-form-item>

        <a-form-item label="ServiceImpl父类路径" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['superServiceImplClass', {rules: [{required: true, min: 3, message: '请输入路径！'}]}]" />
        </a-form-item>

        <a-form-item label="Entity 父类路径" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['superEntityClass', {rules: [{required: true, min: 3, message: '请输入路径！'}]}]" />
        </a-form-item>

        <a-form-item label="Mapper 父类路径" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['superMapperClass', {rules: [{required: true, min: 3, message: '请输入路径！'}]}]" />
        </a-form-item>

        <a-form-item label="基础包路径" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['basePack', {rules: [{required: true, min: 3, message: '请输入路径！'}]}]" />
        </a-form-item>

        <a-form-item label="controller包路径" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['controllerPack', {rules: [{required: true, min: 3, message: '请输入路径！'}]}]" />
        </a-form-item>

        <a-form-item label="service包路径" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['servicePack', {rules: [{required: true, min: 3, message: '请输入路径！'}]}]" />
        </a-form-item>

        <a-form-item label="serviceImpl包路径" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['serviceImplPack', {rules: [{required: true, min: 3, message: '请输入路径！'}]}]" />
        </a-form-item>

        <a-form-item label="mapper包路径" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['mapperPack', {rules: [{required: true, min: 3, message: '请输入路径！'}]}]" />
        </a-form-item>

        <a-form-item label="entity包路径" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['entityPack', {rules: [{required: true, min: 3, message: '请输入路径！'}]}]" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import {
    save,
    getById,
    update,
    saerchAuthor
  } from '@/api/system/online'
  import pick from 'lodash.pick'
  export default {
    data () {
      return {
        labelCol: {
          xs: {
            span: 24
          },
          sm: {
            span: 7
          }
        },
        wrapperCol: {
          xs: {
            span: 24
          },
          sm: {
            span: 13
          }
        },
        modalTitle: '',
        modalOpera: 0, // 0：新增 1：编辑
        visible: false,
        apivisible: false,
        confirmLoading: false,
        parentName: '根目录',
        author: '',
        items: [],
        form: this.$form.createForm(this)
      }
    },
    created () {},
    methods: {
      add (record) {
        this.modal('新建菜单', 0)
        if (record) {
          this.parentName = record.name
          this.apivisible = true
          this.setVal(record.id, record.attributes.type + 1)
        } else {
          this.parentName = '根目录'
          this.apivisible = false
          this.setVal('ROOT', 0)
        }
      },
      setVal (parentId, type) {
        setTimeout(() => {
          this.form.setFieldsValue({
            parentId: parentId
          })
          this.form.setFieldsValue({
            type: type
          })
        }, 100)
      },
      handleChange (id) {
        const sitems = this.items
        let obj = null
        for (let i = 0; i < sitems.length; i++) {
          if (parseInt(sitems[i].id) === parseInt(id)) {
            obj = sitems[i]
            break
          }
        }
        if (obj) {
          this.author = obj.author
          this.form.setFieldsValue(
            pick(
              obj,
              'apiPosition',
              'modelName',
              'author',
              'position',
              'location',
              'fileOverride',
              'open',
              'superControllerClass',
              'superServiceClass	',
              'superServiceImplClass	',
              'superEntityClass',
              'superMapperClass',
              'basePack',
              'controllerPack	',
              'servicePack	',
              'serviceImplPack	',
              'mapperPack	',
              'entityPack	'

            )
          )
        }
      },
      lostFocus () {
        // alert(33);
      },
      handleSearch (author) {
        saerchAuthor({
          author: author
        }).then(response => {
          this.items = response.res
          if (response.data.length === 0) {
            this.form.setFieldsValue({
              author: author
            })
          }
        })
      },
      edit (record) {
        this.modal('编辑菜单', 1)
        getById({
          id: record.id
        }).then(response => {
          const result = response.res
          this.apivisible = result.type !== 0
          this.parentName = result.parentName
          setTimeout(() => {
            this.form.setFieldsValue(
              pick(
                result,
                'id',
                'apiPosition',
                'modelName',
                'author',
                'position',
                'location',
                'fileOverride',
                'open',
                'superControllerClass',
                'superServiceClass',
                'superServiceImplClass',
                'superEntityClass',
                'superMapperClass',
                'basePack',
                'controllerPack',
                'servicePack',
                'serviceImplPack',
                'mapperPack',
                'entityPack'
              )
            )
          }, 200)
        })
      },
      modal (title, opera) {
        this.modalTitle = title
        this.modalOpera = opera
        this.visible = true
        this.form.resetFields()
      },
      handleSubmit () {
        const {
          form: {
            validateFields
          }
        } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            if (this.modalOpera === 0) {
              save(values).then(response => {
                if (response.code === 0) {
                  this.$message.info(response.msg)
                  this.$emit('ok')
                  this.handleCancel()
                } else {
                  this.$message.error(response.msg)
                }
              })
            } else if (this.modalOpera === 1) {
              update(values).then(response => {
                if (response.code === 0) {
                  this.$message.info(response.msg)
                  this.$emit('ok')
                  this.handleCancel()
                } else {
                  this.$message.error(response.msg)
                }
              })
            }
          }
          this.confirmLoading = false
        })
      },
      handleCancel () {
        this.visible = false
        this.confirmLoading = false
      }
    }
  }
</script>
